import { api as viewerApi } from 'v-viewer';

// 表格表单时间范围处理
export const handleRangeTimes = (params: any) => {
  if (!params['RangeTimes']) return params;
  const beginDate = params['RangeTimes']
    ? params['RangeTimes'][0].format('YYYY-MM-DD')
    : '';
  const endDate = params['RangeTimes']
    ? params['RangeTimes'][1].format('YYYY-MM-DD')
    : '';
  delete params['RangeTimes'];
  params.beginDate = beginDate;
  params.endDate = endDate;
  return params;
};

// 获取文件名
export const getFileNameWithoutExtension = (path: string) => {
  // 使用正则表达式匹配最后一个 / 后面的所有字符，但不包括最后一个点（.）之后的内容
  const match = path.match(/[^/]+(?=\.[^./]+$|$)/);
  return match ? match[0].replace(/\.[^/.]+$/, '') : null;
};

// 转大驼峰
export const toPascalCase = (str: any) => {
  // 将连字符或下划线替换为空格，以便后续处理
  const words = str.replaceAll(/[-_]/g, ' ').split(' ');

  // 将每个单词的首字母大写，并将其余部分保持原样
  const pascalCaseWords = words.map((word: any) => {
    if (word) {
      return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
    }
    return word;
  });

  // 将处理后的单词拼接成一个新的字符串
  return pascalCaseWords.join('');
};

// 图片预览
interface PreviewImgOptions {
  url: string;
  index: number;
  images: any[];
}
export const previewImgObject = ({ url, index, images }: PreviewImgOptions) => {
  // Or you can just import the api method and call it.
  const $viewer = viewerApi({
    options: {
      toolbar: true,
      url,
      initialViewIndex: index,
    },
    images,
  });
};

// 判断有没有书名号 并添加书名号
export const handleBookName = (bookName: string) => {
  // 使用正则表达式匹配中文书名号《》
  const regex = /《[^》]*》/;
  bookName = regex.test(bookName) ? bookName : `《${bookName}》`;
  return bookName;
};
